﻿using System.Linq;
using System.Web.Http;

namespace CodeSystem.Controllers
{
    public class CodeController : BaseController
    {
        [HttpGet]
        public IHttpActionResult GetCodes(int page=1,int limit=30,string tag=null)
        {
            using (var db = new EfDbContext())
            {
                
                var list = db.CodeModels.ToList();
                if (!string.IsNullOrEmpty(tag))
                {
                    list = list.Where(x => x.KeyWords.Contains(tag)).ToList();
                }
                foreach (var item in list)
                {
                    item.WpUrl = "";
                }
                var codes = list.OrderByDescending(x => x.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return Success(list);
            }
        }
        [HttpGet]
        public IHttpActionResult SearchCodes(string text)
        {
            using (var db = new EfDbContext())
            {
                var list = db.CodeModels.Where(x => x.KeyWords.Contains(text)).ToList();
                foreach (var item in list)
                {
                    item.WpUrl = "";
                }
                return Success(list);
            }
        }
        [HttpGet]
        public IHttpActionResult GetCodeDetail(int id)
        {
            using (var db = new EfDbContext())
            {
                var code = db.CodeModels.Find(id);
                return Success(code.WpUrl);
            }
        }
    }
}
;